<?php
// 【最终完整版】m-merchant/account_data.php

header('Content-Type: application/json; charset=utf-8');

// --- Token 验证逻辑 ---
function verify_token($token) {
    $secret_key = 'kkcc.vip-is-the-best-!@#$%';
    if (!$token) return null; $token_parts = explode('.', $token);
    if (count($token_parts) !== 3) return null; list($h, $p, $s) = $token_parts;
    $sig = base64_decode(str_replace(['-','_'],['+','/'], $s));
    $exp_sig = hash_hmac('sha256', $h.".".$p, $secret_key, true);
    if (!hash_equals($exp_sig, $sig)) return null;
    $payload = json_decode(base64_decode(str_replace(['-','_'],['+','/'], $p)), true);
    if ($payload === null || ($payload['exp']??0) < time()) return null;
    return $payload['data'];
}
function get_authorization_header() {
    if (isset($_SERVER['Authorization'])) return trim($_SERVER["Authorization"]);
    if (isset($_SERVER['HTTP_AUTHORIZATION'])) return trim($_SERVER["HTTP_AUTHORIZATION"]);
    if (function_exists('getallheaders')) { $h = getallheaders(); if (isset($h['Authorization'])) return trim($h['Authorization']); }
    return null;
}
$user_data = verify_token(str_replace('Bearer ', '', get_authorization_header()));
if ($user_data === null) { http_response_code(401); echo json_encode(['status' => -99, 'msg' => '登录失效']); exit(); }

include_once("../untils/conn.php");
mysqli_query($con, "set names utf8");

$proxy_acc_safe = mysqli_real_escape_string($con, $user_data['proxy_acc']);
$response = ['status' => -1, 'msg' => '无法获取账户信息'];

$sql = "SELECT proxy_alipay, proxy_alipay_acc, proxy_yihname, proxy_yhnum, proxy_bank_name, proxy_bank_zh FROM proxy WHERE proxy_acc = ?";
$stmt = mysqli_prepare($con, $sql);
mysqli_stmt_bind_param($stmt, "s", $proxy_acc_safe);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);

if ($result && $row = mysqli_fetch_assoc($result)) {
    $response = [
        'status' => 0,
        'data' => [
            'alipay_name' => $row['proxy_alipay'],
            'alipay_acc'  => $row['proxy_alipay_acc'],
            'bank_card' => [
                'name'      => $row['proxy_yihname'],
                'number'    => $row['proxy_yhnum'],
                'bank_name' => $row['proxy_bank_name'],
                'branch'    => $row['proxy_bank_zh']
            ]
        ]
    ];
}
mysqli_stmt_close($stmt);

echo json_encode($response);
mysqli_close($con);
?>